/*
 * CrearRubro.java
 *
 * Created on 12-Marz-2009
 */
package investigaciones_itm.Presupuestos;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Body;
import com.sun.webui.jsf.component.Calendar;
import com.sun.webui.jsf.component.Form;
import com.sun.webui.jsf.component.Head;
import com.sun.webui.jsf.component.Html;
import com.sun.webui.jsf.component.Link;
import com.sun.webui.jsf.component.Listbox;
import com.sun.webui.jsf.component.Page;
import com.sun.webui.jsf.component.PanelLayout;
import com.sun.webui.jsf.component.Tab;
import com.sun.webui.jsf.component.TabSet;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.DefaultTableDataProvider;
import javax.faces.FacesException;
import investigaciones_itm.RequestBean1;
import investigaciones_itm.ApplicationBean1;
import investigaciones_itm.SessionBean1;
import itm.bo.presupuestos.PresupuestosBO;
import itm.exception.ControlException;
import itm.vo.presupuestos.presupuestosVO;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.component.html.HtmlMessages;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import com.sun.webui.jsf.model.Option;
import itm.vo.presupuestos.ComponenteVO;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @author Juan Carlos Preciado
 */
public class CrearRubro extends AbstractPageBean {

    private DefaultTableDataProvider defaultTableDataProvider = new DefaultTableDataProvider();

    public DefaultTableDataProvider getDefaultTableDataProvider() {
        return defaultTableDataProvider;
    }

    public void setDefaultTableDataProvider(DefaultTableDataProvider dtdp) {
        this.defaultTableDataProvider = dtdp;
    }
    private HtmlPanelGrid gridPanel2 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel2() {
        return gridPanel2;
    }

    public void setGridPanel2(HtmlPanelGrid hpg) {
        this.gridPanel2 = hpg;
    }
    private HtmlPanelGrid gridPanelButones = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelButones() {
        return gridPanelButones;
    }

    public void setGridPanelButones(HtmlPanelGrid hpg) {
        this.gridPanelButones = hpg;
    }
    private Listbox listboxComponentes = new Listbox();

    public Listbox getListboxComponentes() {
        return listboxComponentes;
    }

    public void setListboxComponentes(Listbox l) {
        this.listboxComponentes = l;
    }

    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private Page page1 = new Page();

    public Page getPage1() {
        return page1;
    }

    public void setPage1(Page p) {
        this.page1 = p;
    }
    private Html html1 = new Html();

    public Html getHtml1() {
        return html1;
    }

    public void setHtml1(Html h) {
        this.html1 = h;
    }
    private Head head1 = new Head();

    public Head getHead1() {
        return head1;
    }

    public void setHead1(Head h) {
        this.head1 = h;
    }
    private Link link1 = new Link();

    public Link getLink1() {
        return link1;
    }

    public void setLink1(Link l) {
        this.link1 = l;
    }
    private Body body1 = new Body();

    public Body getBody1() {
        return body1;
    }

    public void setBody1(Body b) {
        this.body1 = b;
    }
    private Form form1 = new Form();

    public Form getForm1() {
        return form1;
    }

    public void setForm1(Form f) {
        this.form1 = f;
    }
    private HtmlPanelGrid gridPanel1 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel1() {
        return gridPanel1;
    }

    public void setGridPanel1(HtmlPanelGrid hpg) {
        this.gridPanel1 = hpg;
    }
    private TabSet tabSet1 = new TabSet();

    public TabSet getTabSet1() {
        return tabSet1;
    }

    public void setTabSet1(TabSet ts) {
        this.tabSet1 = ts;
    }
    private Tab tabCrearRubro = new Tab();

    public Tab getTabCrearRubro() {
        return tabCrearRubro;
    }

    public void setTabCrearRubro(Tab t) {
        this.tabCrearRubro = t;
    }
    private List<presupuestosVO> listaRubros = new ArrayList<presupuestosVO>();

    public List<presupuestosVO> getListaRubros() {
        return listaRubros;
    }

    public void setListaRubros(List<presupuestosVO> listaRubros) {
        this.listaRubros = listaRubros;
    }
    private PanelLayout panelCrearrubro = new PanelLayout();

    public PanelLayout getPanelCrearrubro() {
        return panelCrearrubro;
    }

    public void setPanelCrearrubro(PanelLayout pl) {
        this.panelCrearrubro = pl;
    }
    private HtmlPanelGrid panelPrincipalR = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelPrincipalR() {
        return panelPrincipalR;
    }

    public void setPanelPrincipalR(HtmlPanelGrid hpg) {
        this.panelPrincipalR = hpg;
    }
    private HtmlPanelGrid panelNombre = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelNombre() {
        return panelNombre;
    }

    public void setPanelNombre(HtmlPanelGrid hpg) {
        this.panelNombre = hpg;
    }
    private HtmlOutputText labelNombre = new HtmlOutputText();

    public HtmlOutputText getLabelNombre() {
        return labelNombre;
    }

    public void setLabelNombre(HtmlOutputText hot) {
        this.labelNombre = hot;
    }
    private HtmlPanelGrid p3 = new HtmlPanelGrid();

    public HtmlPanelGrid getP3() {
        return p3;
    }

    public void setP3(HtmlPanelGrid hpg) {
        this.p3 = hpg;
    }
    private HtmlOutputText labelMision = new HtmlOutputText();

    public HtmlOutputText getLabelMision() {
        return labelMision;
    }

    public void setLabelMision(HtmlOutputText hot) {
        this.labelMision = hot;
    }
    private HtmlPanelGrid p4 = new HtmlPanelGrid();

    public HtmlPanelGrid getP4() {
        return p4;
    }

    public void setP4(HtmlPanelGrid hpg) {
        this.p4 = hpg;
    }
    private HtmlInputTextarea areaMision = new HtmlInputTextarea();

    public HtmlInputTextarea getAreaMision() {
        return areaMision;
    }

    public void setAreaMision(HtmlInputTextarea hit) {
        this.areaMision = hit;
    }
    private HtmlPanelGrid p5 = new HtmlPanelGrid();

    public HtmlPanelGrid getP5() {
        return p5;
    }

    public void setP5(HtmlPanelGrid hpg) {
        this.p5 = hpg;
    }
    private HtmlOutputText labelVision = new HtmlOutputText();

    public HtmlOutputText getLabelVision() {
        return labelVision;
    }

    public void setLabelVision(HtmlOutputText hot) {
        this.labelVision = hot;
    }
    private HtmlPanelGrid p6 = new HtmlPanelGrid();

    public HtmlPanelGrid getP6() {
        return p6;
    }

    public void setP6(HtmlPanelGrid hpg) {
        this.p6 = hpg;
    }
    private HtmlInputTextarea areaVision = new HtmlInputTextarea();

    public HtmlInputTextarea getAreaVision() {
        return areaVision;
    }

    public void setAreaVision(HtmlInputTextarea hit) {
        this.areaVision = hit;
    }
    private HtmlPanelGrid p7 = new HtmlPanelGrid();

    public HtmlPanelGrid getP7() {
        return p7;
    }

    public void setP7(HtmlPanelGrid hpg) {
        this.p7 = hpg;
    }
    private HtmlOutputText labelPolitica = new HtmlOutputText();

    public HtmlOutputText getLabelPolitica() {
        return labelPolitica;
    }

    public void setLabelPolitica(HtmlOutputText hot) {
        this.labelPolitica = hot;
    }
    private HtmlPanelGrid p8 = new HtmlPanelGrid();

    public HtmlPanelGrid getP8() {
        return p8;
    }

    public void setP8(HtmlPanelGrid hpg) {
        this.p8 = hpg;
    }
    private HtmlInputTextarea areaPoliticas = new HtmlInputTextarea();

    public HtmlInputTextarea getAreaPoliticas() {
        return areaPoliticas;
    }

    public void setAreaPoliticas(HtmlInputTextarea hit) {
        this.areaPoliticas = hit;
    }
    private HtmlOutputText labelFecha = new HtmlOutputText();

    public HtmlOutputText getLabelFecha() {
        return labelFecha;
    }

    public void setLabelFecha(HtmlOutputText hot) {
        this.labelFecha = hot;
    }
    private Calendar calendarioFechaRubro = new Calendar();

    public Calendar getCalendarioFechaRubro() {
        return calendarioFechaRubro;
    }

    public void setCalendarioFechaRubro(Calendar c) {
        this.calendarioFechaRubro = c;
    }
    private HtmlPanelGrid panelBoton = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelBoton() {
        return panelBoton;
    }

    public void setPanelBoton(HtmlPanelGrid hpg) {
        this.panelBoton = hpg;
    }
    private HtmlCommandButton botonGuardar = new HtmlCommandButton();

    public HtmlCommandButton getBotonGuardar() {
        return botonGuardar;
    }

    public void setBotonGuardar(HtmlCommandButton hcb) {
        this.botonGuardar = hcb;
    }
    private HtmlCommandButton botonCancelar = new HtmlCommandButton();

    public HtmlCommandButton getBotonCancelar() {
        return botonCancelar;
    }

    public void setBotonCancelar(HtmlCommandButton hcb) {
        this.botonCancelar = hcb;
    }
    private HtmlPanelGrid p9 = new HtmlPanelGrid();

    public HtmlPanelGrid getP9() {
        return p9;
    }

    public void setP9(HtmlPanelGrid hpg) {
        this.p9 = hpg;
    }
    private HtmlOutputText labelMail = new HtmlOutputText();

    public HtmlOutputText getLabelMail() {
        return labelMail;
    }

    public void setLabelMail(HtmlOutputText hot) {
        this.labelMail = hot;
    }
    private HtmlPanelGrid p10 = new HtmlPanelGrid();

    public HtmlPanelGrid getP10() {
        return p10;
    }

    public void setP10(HtmlPanelGrid hpg) {
        this.p10 = hpg;
    }
    private HtmlInputText textEmail = new HtmlInputText();

    public HtmlInputText getTextEmail() {
        return textEmail;
    }

    public void setTextEmail(HtmlInputText hit) {
        this.textEmail = hit;
    }
    private UISelectItems listbox1SelectItems = new UISelectItems();

    public UISelectItems getListbox1SelectItems() {
        return listbox1SelectItems;
    }

    public void setListbox1SelectItems(UISelectItems uisi) {
        this.listbox1SelectItems = uisi;
    }
    private UISelectItems listbox2SelectItems = new UISelectItems();

    public UISelectItems getListbox2SelectItems() {
        return listbox2SelectItems;
    }

    public void setListbox2SelectItems(UISelectItems uisi) {
        this.listbox2SelectItems = uisi;
    }
    private HtmlCommandButton buttonComponentes = new HtmlCommandButton();

    public HtmlCommandButton getButtonComponentes() {
        return buttonComponentes;
    }

    public void setButtonComponentes(HtmlCommandButton hcb) {
        this.buttonComponentes = hcb;
    }
    private HtmlMessages messageList1 = new HtmlMessages();

    public HtmlMessages getMessageList1() {
        return messageList1;
    }

    public void setMessageList1(HtmlMessages hm) {
        this.messageList1 = hm;
    }
    private TextField nombreRubro = new TextField();

    public TextField getNombreRubro() {
        return nombreRubro;
    }

    public void setNombreRubro(TextField tf) {
        this.nombreRubro = tf;
    }
    private Tab tabAnexarComponentes = new Tab();

    public Tab getTabAnexarComponentes() {
        return tabAnexarComponentes;
    }

    public void setTabAnexarComponentes(Tab t) {
        this.tabAnexarComponentes = t;
    }
    private PanelLayout layoutPanel1 = new PanelLayout();

    public PanelLayout getLayoutPanel1() {
        return layoutPanel1;
    }

    public void setLayoutPanel1(PanelLayout pl) {
        this.layoutPanel1 = pl;
    }
    private HtmlPanelGrid gridPanelAnexoComponente = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelAnexoComponente() {
        return gridPanelAnexoComponente;
    }

    public void setGridPanelAnexoComponente(HtmlPanelGrid hpg) {
        this.gridPanelAnexoComponente = hpg;
    }
    private HtmlMessages messageList2 = new HtmlMessages();

    public HtmlMessages getMessageList2() {
        return messageList2;
    }

    public void setMessageList2(HtmlMessages hm) {
        this.messageList2 = hm;
    }
    private HtmlOutputText labelNombreComponente1 = new HtmlOutputText();

    public HtmlOutputText getLabelNombreComponente1() {
        return labelNombreComponente1;
    }

    public void setLabelNombreComponente1(HtmlOutputText hot) {
        this.labelNombreComponente1 = hot;
    }
    private TextField textFieldComponente1 = new TextField();

    public TextField getTextFieldComponente1() {
        return textFieldComponente1;
    }

    public void setTextFieldComponente1(TextField tf) {
        this.textFieldComponente1 = tf;
    }
    private HtmlCommandButton buttonAnexarComponente = new HtmlCommandButton();

    public HtmlCommandButton getButtonAnexarComponente() {
        return buttonAnexarComponente;
    }

    public void setButtonAnexarComponente(HtmlCommandButton hcb) {
        this.buttonAnexarComponente = hcb;
    }
    private HtmlCommandButton botonVolver = new HtmlCommandButton();

    public HtmlCommandButton getBotonVolver() {
        return botonVolver;
    }

    public void setBotonVolver(HtmlCommandButton hcb) {
        this.botonVolver = hcb;
    }
    private HtmlMessages messageList3 = new HtmlMessages();

    public HtmlMessages getMessageList3() {
        return messageList3;
    }

    public void setMessageList3(HtmlMessages hm) {
        this.messageList3 = hm;
    }
    private HtmlOutputText labelIDRubro = new HtmlOutputText();

    public HtmlOutputText getLabelIDRubro() {
        return labelIDRubro;
    }

    public void setLabelIDRubro(HtmlOutputText hot) {
        this.labelIDRubro = hot;
    }
    private TextField textFieldIDRubro = new TextField();

    public TextField getTextFieldIDRubro() {
        return textFieldIDRubro;
    }

    public void setTextFieldIDRubro(TextField tf) {
        this.textFieldIDRubro = tf;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    /*    public CrearRubro() {
    }
     */
    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
// Perform initializations inherited from our superclass
        // Perform initializations inherited from our superclass
        super.init();
// Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

// <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
// Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
    // Perform application initialization that must complete
    // *before* managed components are initialized
    // TODO - add your own initialiation code here

    // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
    // Initialize automatically managed components
    // *Note* - this logic should NOT be modified
    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {

        PresupuestosBO rubrosBO = new PresupuestosBO();

        try {
            listaRubros = rubrosBO.listarRubros();

        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public Option[] getAvailableOptions() {
        return availableOptions;
    }

    public void setAvailableOptions(Option[] availableOptions) {
        this.availableOptions = availableOptions;
    }
    private presupuestosVO rubroVO = new presupuestosVO();
    private PresupuestosBO rubroBO = new PresupuestosBO();
    private Option[] availableOptions = null;
    public static int idRubro = 0;

    public String botonGuardarRubro_action() throws Exception {

        String nomRubro = "";
        Date fechaR = null;

        try {

            java.util.Date fechaRegistro = (java.util.Date) calendarioFechaRubro.getValue();
            nomRubro = (String) nombreRubro.getValue();
            if (nomRubro == null || nomRubro.equals("")) {
                error("Por favor ingrese el Nombre del Rubro");
                return null;
            } else if (fechaRegistro == null) {
                error("Por favor ingrese una fecha");
                return null;
            }

            fechaR = new Date(fechaRegistro.getTime());

            rubroVO.setNombreRubro(nomRubro);
            rubroVO.setFechaRegistro(fechaR);
            idRubro = rubroBO.crearRubro(rubroVO);

            if (idRubro == 0) {
                error("El Nombre del Rubro Ya Existe");
            } else {
                textFieldIDRubro.setValue(idRubro);
                info("El Rubro se ha creado correctamente.");
                buttonComponentes.setDisabled(false);
                buttonComponentes.setRendered(true);
                botonGuardar.setDisabled(true);
            }

        //error(errorM);

        } catch (ControlException ce) {
            error("Error al crear el Rubro.");
            ce.getStackTrace();
        }
        return "OK";
    }

    public String buttonComponentes_action() {
        tabAnexarComponentes.setVisible(true);
        tabAnexarComponentes.setRendered(true);
        tabCrearRubro.setDisabled(true);
        tabSet1.setSelected(tabAnexarComponentes.getId().toString());
        return null;
    }

    public String botonCancelar_action() {

        nombreRubro.setValue("");
        calendarioFechaRubro.setValue("");

        return null;
    }

    public String buttonAnexarComponente_action() {
        try {

            String nomComponente = "";
            presupuestosVO componenteNom;
            PresupuestosBO componenteBO;

            componenteBO = new PresupuestosBO();
            componenteNom = new presupuestosVO();

            int resultado;

            nomComponente = (String) textFieldComponente1.getValue();

            if (nomComponente == null || nomComponente.equals("")) {
                error("Por favor Ingrese el nombre del Componente");
                return null;
            }

            componenteNom.setNombreComponente(nomComponente);
            componenteNom.setIdRubro(idRubro);

            resultado = componenteBO.crearComponente(componenteNom);


            if (resultado == 0) {
                error("No se puede adicionar el Componente.");

            } else {
                info("Componente Adicionado correctamente");
            }

            listboxComponentes.setRendered(true);

            String nombreComponente;

            List<ComponenteVO> componentes = new ArrayList<ComponenteVO>();

            componentes = componenteBO.consultaComponentes(idRubro, 0);

            Option[] opcion = new Option[componentes.size()];

            for (int i = 0; i < componentes.size(); i++) {
                nombreComponente = componentes.get(i).getNombreComponente();
                opcion[i] = new Option(nombreComponente);
            }
            availableOptions = opcion;



        } catch (Exception e) {
            error("Error al adicionar el Componente" + e);
        }
        return null;
    }

    public String botonVolver_action() {
        // TODO: Process the button click action. Return value is a navigation
        // case name where null will return to the same page.

        tabSet1.setSelected(tabCrearRubro.getId().toString());
        tabAnexarComponentes.setVisible(false);
        tabAnexarComponentes.setRendered(false);
        botonGuardar.setDisabled(false);
        return null;
    }
}

